import crud.match
from fb.checkResult import CheckResult
from sqlalchemy.orm import Session
from utils.database import SessionLocal
from models.match import Match
class UpdateScoreService:

    def update(self):
        db = SessionLocal()
        checkResult = CheckResult()
        scores_dict = checkResult.get_scors()
        matchs = crud.match.get_no_scores(db)
        print(matchs)
        print(scores_dict)
        for match in matchs:
            score = scores_dict.get(match.fb_id)
            if score:
                performance = self.get_performance(score,match.prediction)
                match.score = score
                match.performance = performance
                crud.match.update(db,match.id,match)



    def get_performance(self,score, prediction):
        performance = ''
        pre = prediction.split(' ')
        home_score = int(score.split('-')[0])
        away_score = int(score.split('-')[1])
        zf = home_score + away_score
        match pre[0]:
            case 'Over':
                if zf - float(pre[1]) == 0:
                    performance = ('PUSH')
                if zf - float(pre[1]) == 0.25:
                    performance = ('Win Half')
                if zf - float(pre[1]) > 0.49:
                    performance = ('Win')
                if zf - float(pre[1]) == -0.25:
                    performance = ('Lose Half')
                if zf - float(pre[1]) < -0.49:
                    performance = ('Lose')
            case 'Under':
                if zf - float(pre[1]) == 0:
                    performance = ('PUSH')
                if zf - float(pre[1]) == -0.25:
                    performance = ('Win Half')
                if zf - float(pre[1]) < -0.49:
                    performance = ('Win')
                if zf - float(pre[1]) == 0.25:
                    performance = ('Lose Half')
                if zf - float(pre[1]) > 0.49:
                    performance = ('Lose')
            case 'Home':
                if home_score + float(pre[1]) - away_score == 0:
                    performance = ('PUSH')
                if home_score + float(pre[1]) - away_score == 0.25:
                    performance = ('Win Half')
                if home_score + float(pre[1]) - away_score > 0.49:
                    performance = ('Win')
                if home_score + float(pre[1]) - away_score == -0.25:
                    performance = ('Lose Half')
                if home_score + float(pre[1]) - away_score < -0.49:
                    performance = ('Lose')
            case 'Away':
                if away_score + float(pre[1]) - home_score == 0:
                    performance = ('PUSH')
                if away_score + float(pre[1]) - home_score == 0.25:
                    performance = ('Win Half')
                if away_score + float(pre[1]) - home_score > 0.49:
                    performance = ('Win')
                if away_score + float(pre[1]) - home_score == -0.25:
                    performance = ('Lose Half')
                if away_score + float(pre[1]) - home_score < -0.49:
                    performance = ('Lose')
        return performance

if __name__ == "__main__":
    service = UpdateScoreService()
    service.update()
